<template>
  <div class="my-page">
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      height="100%"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column
        v-for="(item, index) in config"
        :key="index"
        :prop="item.prop"
        :label="item.label"
      >
      </el-table-column>
      <el-table-column>
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            :placeholder="defineConfig.searchText"
          />
        </template>
        <template slot-scope="scope">
          <el-button
            v-if="defineConfig.isShow"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >{{ defineConfig.leftButtonText }}</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="padding"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="totalNumber"
      @current-change="handlePaginate"
      @prev-click="handlePaginate"
      @next-click="handlePaginate"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "MyContent",
  props: {
    config: {
      default: [],
      type: Array,
    },
    resData: {
      default: [],
      type: Array,
    },
    defineConfig: {
      default: {},
      type: Object,
    },
    totalNumber: {
      default: 1000,
      type: Number,
    },
    pageSize: {
      default: 6,
      type: Number,
    },
  },
  mounted() {
    this.getTableList();
  },
  data() {
    return {
      search: "",
      page: 0,
      multipleSelection: [],
      tableData: [],
    };
  },
  watch: {
    search(newVal) {
      if (newVal && newVal.trim().length > 0) {
        this.tableData = this.resData.filter((data) =>
          data.objectId.toLowerCase().includes(newVal.toLowerCase())
        );
      } else {
        this.getTableList();
      }
    },
  },
  methods: {
    //获取table数据
    getTableList() {
      let skip = this.page * this.pageSize;
      let lastSkip = skip + this.pageSize;
      console.log("resData============", this.resData, skip, lastSkip);
      //***this.tableData = this.resData.slice(skip, lastSkip); => []
      // this.$emit("handleResData", skip, lastSkip);
      // this.tableData = this.resData.slice(skip, lastSkip);
      this.tableData =this.resData
      console.log("this.tableData============", this.tableData);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      let { objectId } = row;
      let text = this.defineConfig.leftButtonText;
      this.$router.push(`/Orders/orderDetail?objectId=${objectId}`);
      this.$store.commit("communication/changePageTextMu", text);
    },
    handleDelete(index, row) {
      let { objectId } = row;
      let idx = this.page * this.pageSize + index;
      //***会删除两条数据,this.tableData是this.resdata的
      // this.tableData.splice(index, 1);
      // this.$emit("handleResData", idx, 1);
      //
      this.tableData.splice(index, 1);
      // this.$H.delete(`/1.1/classes/order/${objectId}`);
    },
    handlePaginate(index) {
      this.page = index - 1;
      this.getTableList();
    },
  },
};
</script>

<style lang="less" scoped>
.my-page {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;

  .goods-img {
    width: 100%;
    height: 100%;
  }
}
.padding {
  padding: 20px 0;
  text-align: center;
}
/deep/.el-table .cell {
  text-align: center;
}
/deep/.el-tooltip__popper {
  width: 120px;
}
</style>